/* ***** BEGIN LICENSE BLOCK *****
 *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Seoprofesional
 *
 * The Initial Developer of the Original Code is
 * Petr Blahoš.
 * Portions created by the Initial Developer are Copyright (C) 2009
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * 
 * ***** END LICENSE BLOCK ***** */

if (typeof Cc == "undefined")
	var Cc = Components.classes;
if (typeof Ci == "undefined")
	var Ci = Components.interfaces;


SeoprofesionalTools = {
consoleService: Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService),
LOG: function(msg) {
  SeoprofesionalTools.consoleService.logStringMessage(msg);
},
DUMP_MEMBERS: function(o) {
	for(var i in o) {
		this.LOG("" + i + ":" + o[i]);
	}
},
cleanHttpsURL: function(url) {
   var match = url.match(/^https:\/\/[^/]+\//);
   return match ? match[0] : url;
},
format_date: function(d, fmt, monthNames) {
    var leftPad = function(n) {
	n = "" + n;
	return n.length == 1 ? "0" + n : n;
    };
    
    var r = [];
    var escape = false;
    if (monthNames == null)
	monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    for (var i = 0; i < fmt.length; ++i) {
	var c = fmt.charAt(i);
	
	if (escape) {
	    switch (c) {
	    case 'h': c = "" + d.getUTCHours(); break;
	    case 'H': c = leftPad(d.getUTCHours()); break;
	    case 'M': c = leftPad(d.getUTCMinutes()); break;
	    case 'S': c = leftPad(d.getUTCSeconds()); break;
	    case 'd': c = "" + d.getUTCDate(); break;
	    case 'm': c = "" + (d.getUTCMonth() + 1); break;
	    case 'y': c = "" + d.getUTCFullYear(); break;
	    case 'b': c = "" + monthNames[d.getUTCMonth()]; break;
	    }
	    r.push(c);
	    escape = false;
	}
	else {
	    if (c == "%")
		escape = true;
	    else
		r.push(c);
	}
    }
    return r.join("");
},
get_domain: function(uri) {
	var ss = uri.indexOf("//");
	if (-1!=ss) {
		uri = uri.substr(ss+2);
	} // else: wierd, looks like the uri doesn't start with something://
	ss = uri.indexOf("/");
	if (-1!=ss) {
		uri = uri.substr(0, ss);
	}
	return uri;
},
getHttpDomain: function(url) {
    // returns https?://domain.xx/
    var match = url.match(/^https?:\/\/[^/]+\//);
    return match ? match[0] : url;
},

/**
 * Set a node's attribute.
 */
set_attr: function(id, name, val) {
	document.getElementById(id).setAttribute(name, val);
},
/**
 * Remove all subnodes.
 */
clear_subnodes: function(el) {
	while(null!=el.firstChild) {
		el.removeChild(el.firstChild);
	}
},
/**
 * Appends menu item to the menu.
 */
addmenu: function(menu, label, f) {
	var el = document.createElement("menuitem");
	el.setAttribute("label", label);
	el.setAttribute("oncommand", f);
	menu.appendChild(el);
},

open_uri: function(ev, uri) {
	var windowMediator = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
	var w = windowMediator.getMostRecentWindow("navigator:browser");
	var browser = w.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
	   .getInterface(Components.interfaces.nsIWebNavigation)
	   .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
	   .rootTreeItem
	   .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
	   .getInterface(Components.interfaces.nsIDOMWindow).gBrowser;
	
	browser.openUILink(uri, ev, false, true);
},
_x_sentinel: 0
}

